package beautysalonsystem.service;

import beautysalonsystem.mapper.OrderMapper;
import beautysalonsystem.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    public static final Map<Integer, LocalTime> END_TIME_MAP = new HashMap<>();
    static {
        END_TIME_MAP.put(1, LocalTime.of(11, 30));
        END_TIME_MAP.put(2, LocalTime.of(12, 30));
        END_TIME_MAP.put(3, LocalTime.of(14, 0));
        END_TIME_MAP.put(4, LocalTime.of(15, 0));
        END_TIME_MAP.put(5, LocalTime.of(16, 0));
        END_TIME_MAP.put(6, LocalTime.of(17, 0));
        END_TIME_MAP.put(7, LocalTime.of(18, 0));
        END_TIME_MAP.put(8, LocalTime.of(19, 0));
        END_TIME_MAP.put(9, LocalTime.of(20, 0));
        END_TIME_MAP.put(10, LocalTime.of(21, 0));
    }

    //判断某个时间段是否可以被预定
    public int isTimeSlotAvailable(Order order){
        //1.如果当前时间已经超过预定时间
        LocalDate orderDate = order.getDate().toLocalDate();
        LocalDate today = LocalDate.now();
        if (orderDate.isBefore(today)) {
            return -1;
        }

        // 2. 同一天的情况下，判断时间是否超过 cell 对应的结束时间
        if (orderDate.equals(today)) {
            LocalTime nowTime = LocalTime.now();
            LocalTime endTime = END_TIME_MAP.get(order.getCell());
            if (endTime != null && nowTime.isAfter(endTime)) {
                return -1; // 当前时间已经超过该时段，不可预约
            }
        }

        //3.如果预定的时间已经被预定
        Order existing = orderMapper.getOrderByDateAndCell(order.getDate(), order.getCell());
        if (existing != null) {
            return -2;
        }
        return 1;
    }
}
